package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.service.UploadService;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/supplier")
public class SupplierController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierController.class);
    @Autowired
    private SupplierService supplierService;
    @Autowired
    @Qualifier("toLocalUploadService")
    private UploadService uploadService;

    @RequestMapping("/page")
    public String page(Supplier condition, Model model, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<Supplier> page = null;
        try {
            page = supplierService.findPage(condition, pageNo, pageSize);
            model.addAttribute("pageInfo", page);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            LOGGER.error("执行SupplierController.page查询入库记录列表异常", e);
            throw new RuntimeException(e);
        }

        return "supplier/page";
    }


    /**
     * 先从page.jsp页面跳转到这个controller,再由这跳转到添加页面
     */
    @GetMapping("/save")
    public String toSave(Model model) {
        return "supplier/save";
    }

    @PostMapping("/save")
    public String doSave(Supplier supplier, MultipartFile bizPic) {
        //允许上传图片后缀
        String[] allowImageExt = {"jpg", "jpeg", "png", "gif", "bmp"};
        try {
            String bizPicPath = uploadService.uploadFile(bizPic, allowImageExt);
            supplier.setBizPicPath(bizPicPath);
            //通过父类的session方法获取登录用户,由于泛型自动转换,无需强转
            SysUser loginUser = session(Constants.SESSION_USER);
            if (supplier.getId() == null) {
                //添加用户,关联创建用户为当前登录用户
                supplier.setCreatedUserId(loginUser.getId());
                supplier.setCreatedTime(new Date());
            } else {
                supplier.setUpdatedUserId(loginUser.getId());
                supplier.setUpdatedTime(new Date());
            }
            //新增或修改供货商
            boolean ok = supplierService.saveOrUpdate(supplier);
            if (ok) {
                //重定向到供应商列表页面
                return redirect("/main/supplier/page");
            }
        } catch (Exception e) {
            LOGGER.error("SupplierController中doSave方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return "supplier/save";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model) {
        Supplier supplier = null;
        try {
            //根据id查询用户信息
            supplier = supplierService.findById(id);

            model.addAttribute("supplier", supplier);
        } catch (Exception e) {
            LOGGER.error("SupplierController中view方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return "supplier/view";
    }

    @GetMapping("/edit")
    public String toEdit(Model model, @RequestParam("id") Long id) {
        Supplier supplier = null;
        try {
            //根据id查询用户信息
            supplier = supplierService.findById(id);
            model.addAttribute("supplier", supplier);
        } catch (Exception e) {
            LOGGER.error("SupplierController中toEdit方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return "supplier/save";
    }


    @ResponseBody
    @DeleteMapping("/del/{id}")
    public Map del(@PathVariable("id") Long id) {
        HashMap<String, Object> resultMap = new HashMap<>();
        try {
            //根据id删除用户信息
            boolean ok = supplierService.deleteById(id);
            if (ok) {
                resultMap.put("code", 200);
                resultMap.put("data", "数据删除成功");
            } else {
                resultMap.put("code", 500);
            }

        } catch (Exception e) {
            LOGGER.error("SysUserController中de方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return resultMap;
    }
}
